import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/layout'
import { getToken } from '@/utils/auth'

Vue.use(Router)

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/register/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '主页', icon: 'el-icon-crop', requiresAuth: true }
    }]
  },

  {
    path: '/search',
    component: () => import('@/views/search/index'),
    name: 'Search',
    meta: { title: '搜索结果' }
  },

  {
    path: '/shop',
    component: Layout,
    redirect: '/shop/a',
    name: 'Shop',
    meta: { title: '店铺管理', icon: 'el-icon-goods' },
    children: [
      {
        path: 'a',
        name: 'AShop',
        component: () => import('@/views/ashop/as.vue'),
        meta: { title: 'A店铺', icon: 'el-icon-fork-spoon' }
      },
      {
        path: 'a/dish/:id',
        name: 'ADishDetail',
        component: () => import('@/views/ashop/dishdetail.vue'),
        hidden: true,
        meta: { title: 'A店铺菜品详情', activeMenu: '/shop/a' }
      },
      {
        path: 'b',
        name: 'BShop',
        component: () => import('@/views/bshop/bs.vue'),
        meta: { title: 'B店铺', icon: 'el-icon-knife-fork' }
      },
      {
        path: 'b/dish/:id',
        name: 'BDishDetail',
        component: () => import('@/views/bshop/dishdetail.vue'),
        hidden: true,
        meta: { title: 'B店铺菜品详情', activeMenu: '/shop/b' }
      }
    ]
  },
  {
    path: '/information',
    component: () => import('@/views/information/index.vue'),
    name: 'Information',
    meta: {
      title: '个人中心',
      requiresAuth: true
    }
  }

  ,
  {
    path: '/external-link',
    component: Layout,
    children: [
      {
        path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
        meta: { title: '外部链接', icon: 'link' }
      }
    ]
  },
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export default router
